package main

import (
	"encoding/json"
	"fmt"
	"io/ioutil"
	"log"
	"net/http"
	"strings"
	"time"
)

var DefaultHeader = map[string][]string{
"Host":         {"cat-match.easygame2021.com"},
"Connection":   {"keep-alive"},
"user-agent":   {"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36 MicroMessenger/7.0.4.501 NetType/WIFI MiniProgramEnv/Windows WindowsWechat/WMPF"},
"Content-Type": {"application/json"},
"Accept":       {"*"},
"Referer": {"https://servicewechat.com/wx141bfb9b73c970a9/19/index.html"},
}
type InfoModel struct {
	Data struct {
		Avatar          string      `json:"avatar"`
		ChargeFirstTime int         `json:"charge_first_time"`
		ChargeLastTime  int         `json:"charge_last_time"`
		ChargeTimes     int         `json:"charge_times"`
		ChargeTotal     int         `json:"charge_total"`
		CreatedAt       time.Time   `json:"created_at"`
		Gender          int         `json:"gender"`
		Id              string      `json:"id"`
		LastLoginTime   interface{} `json:"last_login_time"`
		LastLogoutTime  interface{} `json:"last_logout_time"`
		NickName        string      `json:"nick_name"`
		Role            int         `json:"role"`
		Uid             int         `json:"uid"`
		UpdatedAt       time.Time   `json:"updated_at"`
		WxOpenId        string      `json:"wx_open_id"`
		WxUnionId       string      `json:"wx_union_id"`
	} `json:"data"`
	ErrCode int    `json:"err_code"`
	ErrMsg  string `json:"err_msg"`
}

type UidModle struct {
	Data struct {
		Openid string `json:"openid"`
		Token  string `json:"token"`
		Uid    int    `json:"uid"`
	} `json:"data"`
	ErrCode int    `json:"err_code"`
	ErrMsg  string `json:"err_msg"`
}
type Config struct {
	Uid string
	Times int
}
const t = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE2OTQ0ODM1MjEsIm5iZiI6MTY2MzM4MTMyMSwiaWF0IjoxNjYzMzc5NTIxLCJqdGkiOiJDTTpjYXRfbWF0Y2g6bHQxMjM0NTYiLCJvcGVuX2lkIjoiIiwidWlkIjo1MDcyMDYyNSwiZGVidWciOiIiLCJsYW5nIjoiIn0.p4e256Sfk3Syt_AqBN-TMFGnefDKf_XQFfdE-v_SxoY"
const userInfoApi = "https://cat-match.easygame2021.com/sheep/v1/game/user_info?"
const loginApi = "https://cat-match.easygame2021.com/sheep/v1/user/login_tourist"

func GetToken(uid string) string  {

	urlInfo:=fmt.Sprintf("%suid=%s&t=%s", userInfoApi, uid, t)
	response, err := http.Get(urlInfo)
	if err != nil {
		log.Fatal("error:", err)
	}

	bodyText, err := ioutil.ReadAll(response.Body)
	var info InfoModel
	err = json.Unmarshal(bodyText, &info)
	if err != nil {
		log.Fatal("error:", err)
	}
	openid :=info.Data.WxOpenId
	client:=&http.Client{}
	logBody :=map[string]string{
		"uuid":openid,
	}
	b, err := json.Marshal(logBody)
	if err != nil {
		log.Fatal(err)
	}
	req, err := http.NewRequest("POST", loginApi,  strings.NewReader(string(b)))
	if err != nil {
		log.Fatal("error:", err)
	}
	req.Header=DefaultHeader
	req.Header.Add("t",t)
	res, err := client.Do(req)
	if err != nil {
		log.Fatal("error:", err)
	}


	bodyText, err = ioutil.ReadAll(res.Body)
	var uidRes UidModle
	err = json.Unmarshal(bodyText, &uidRes)
	if err != nil {
		log.Fatal("error:", err)
	}

	return uidRes.Data.Token
}